package com.cloudcc.mobile.bull.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cloudcc.mobile.R;
import com.cloudcc.mobile.bull.bean.CheckAdapterkBean;
import com.cloudcc.mobile.bull.listener.OnSignedSuccess;
import com.cloudcc.mobile.util.DateUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 金鑫 on 2019/8/28.
 */

public class AdapterDate extends BaseAdapter {

    private Context context;
    private List<Integer> days = new ArrayList<>();
    //日历数据
    private int status = -1;
    //选中日期
    private OnSignedSuccess onSignedSuccess;
    //签到成功的回调方法，相应的可自行添加签到失败时的回调方法

    //签到数据
    ArrayList<CheckAdapterkBean> checkAdapterkBeans = new ArrayList<>();

    public AdapterDate(final ArrayList<CheckAdapterkBean> checkAdapterkBeans,int year,int month ,Context context) {
        this.checkAdapterkBeans = checkAdapterkBeans;
        this.context = context;
        for (int i = 0; i < checkAdapterkBeans.size(); i++) {
            if(checkAdapterkBeans.get(i).getDays() == DateUtil.getCurrentDay(year,month))
            {
                status = i;
            }
        }
    }

    @Override
    public int getCount() {
        return checkAdapterkBeans.size();
    }

    @Override
    public Object getItem(int i) {
        return checkAdapterkBeans.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if(view==null){
            view = LayoutInflater.from(context).inflate(R.layout.item_gv,null);
            viewHolder = new ViewHolder();
            view.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.tv = (TextView) view.findViewById(R.id.tvWeek);
        viewHolder.rlItem = (RelativeLayout) view.findViewById(R.id.rlItem);
        viewHolder.ivCountOne = (ImageView) view.findViewById(R.id.ivCountOne);
        viewHolder.ivCountTwo = (ImageView) view.findViewById(R.id.ivCountTwo);
        viewHolder.ivCountThree = (ImageView) view.findViewById(R.id.ivCountThree);
        viewHolder.ivCountZero = (ImageView)view.findViewById(R.id.ivCountZero);

        viewHolder.tv.setText(checkAdapterkBeans.get(i).getDays()+"");
        if(checkAdapterkBeans.get(i).getDays()==0){
            viewHolder.rlItem.setVisibility(View.GONE);
        }

        int count = 0;
        if(null!=checkAdapterkBeans.get(i).getCheckinHistoryBean())
        {
            if (null != checkAdapterkBeans.get(i).getCheckinHistoryBean().getBgsj()) {
                count++;
            }
            if (null != checkAdapterkBeans.get(i).getCheckinHistoryBean().getZwbgsj()) {
                count++;
            }
            if (null != checkAdapterkBeans.get(i).getCheckinHistoryBean().getXbbgsj()) {
                count++;
            }
        }

        switch (count){
            case 0:
                viewHolder.ivCountZero.setVisibility(View.INVISIBLE);
                viewHolder.ivCountOne.setVisibility(View.GONE);
                viewHolder.ivCountTwo.setVisibility(View.GONE);
                viewHolder.ivCountThree.setVisibility(View.GONE);
                break;
            case 1:
                viewHolder.ivCountZero.setVisibility(View.GONE);
                viewHolder.ivCountOne.setVisibility(View.VISIBLE);
                viewHolder.ivCountTwo.setVisibility(View.GONE);
                viewHolder.ivCountThree.setVisibility(View.GONE);
                break;
            case 2:
                viewHolder.ivCountZero.setVisibility(View.GONE);
                viewHolder.ivCountOne.setVisibility(View.GONE);
                viewHolder.ivCountTwo.setVisibility(View.VISIBLE);
                viewHolder.ivCountThree.setVisibility(View.GONE);
                break;
            case 3:
                viewHolder.ivCountZero.setVisibility(View.GONE);
                viewHolder.ivCountOne.setVisibility(View.GONE);
                viewHolder.ivCountTwo.setVisibility(View.GONE);
                viewHolder.ivCountThree.setVisibility(View.VISIBLE);
                break;

        }

        if(i == status){
            viewHolder.tv.setTextColor(Color.parseColor("#FD0000"));
        }else{
            viewHolder.tv.setTextColor(Color.parseColor("#666666"));
        }

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                    if(status != i)
                    {
                        status = i;
                        notifyDataSetChanged();
                        if(onSignedSuccess!=null){
                            onSignedSuccess.OnSignedSuccess(i);
                        }
                    }
            }
        });
        return view;
    }

    class ViewHolder{
        RelativeLayout rlItem;
        TextView tv;
//        ImageView ivStatus;
        ImageView ivCountOne,ivCountTwo,ivCountThree,ivCountZero;
    }

    public void setOnSignedSuccess(OnSignedSuccess onSignedSuccess){
        this.onSignedSuccess = onSignedSuccess;
    }
}
